import sys

from PyQt6 import uic
from PyQt6.QtWidgets import QApplication, QWidget, QLineEdit, QTextEdit, QPushButton
from PyQt6.uic import loadUi

from scripts import db_utils
from ui.widgets.BaseDialog import BaseDialog


class CommonPromptsDialog(BaseDialog):

    def __init__(self, callback):
        super().__init__(callback)
        self.content_layout = None
        self.width_et: QLineEdit = None
        self.height_et: QLineEdit = None
        self.positive_prompt: QTextEdit = None
        self.negative_prompt: QTextEdit = None
        self.cancel_btn: QPushButton = None
        self.save_btn: QPushButton = None
        loadUi('./source/dialogs/common_prompt_page.ui', self)

        self.initUI()

    def initUI(self):
        self.save_btn.clicked.connect(self.save)  # 点击Yes时接受对话框
        self.cancel_btn.clicked.connect(self.reject)  # 点击Yes时接受对话框

        self.project_config = db_utils.load_project_config()

        self.width_et.setText(str(self.project_config['width']))
        self.height_et.setText(str(self.project_config['height']))
        self.positive_prompt.setText(self.project_config['positive_prompt'])
        self.negative_prompt.setText(self.project_config['negative_prompt'])
        pass

    def save(self):
        self.project_config['width'] = self.width_et.text()
        self.project_config['height'] = self.height_et.text()
        self.project_config['positive_prompt'] = self.positive_prompt.toPlainText()
        self.project_config['negative_prompt'] = self.negative_prompt.toPlainText()
        db_utils.save_project_config(self.project_config)
        self.accept()
